package com.achobeta.recruitment.service.impl.manager;

import com.achobeta.recruitment.common.ResultCode;
import com.achobeta.recruitment.common.Rule;
import com.achobeta.recruitment.dao.manager.ManagerMapper;
import com.achobeta.recruitment.model.Manager;
import com.achobeta.recruitment.service.manager.ManagerService;
import com.achobeta.recruitment.utils.CheckUtils;
import com.achobeta.recruitment.utils.LimitUtils;
import com.achobeta.recruitment.utils.MD5Utils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 马拉圈
 * Date: 2023-10-31
 * Time: 13:18
 */
@Slf4j
@Service
public class ManagerServiceImpl implements ManagerService {

    @Autowired
    private ManagerMapper managerMapper;


    @Override
    public Manager selectManagerByName(String managerName) {
        // 校验
        CheckUtils.checkStringParams(ResultCode.FAILED_PARAMS_VALIDATE, managerName);
        // 查询
        Manager manager = managerMapper.selectByName(managerName);
        // 空也返回
        return manager;
    }

    @Override
    public Manager selectManagerById(Integer id) {
        // 校验
        CheckUtils.checkNULL(id);
        // 查询
        Manager manager = managerMapper.selectByPrimaryKey(id);
        // 无论如何都返回
        return manager;
    }

    @Override
    public void register(String managerName, String password) {
        // 校验
        CheckUtils.checkStringParams(ResultCode.FAILED_PARAMS_VALIDATE,
                managerName, password);
        // 校验是否存在
        Manager checkManager = selectManagerByName(managerName);
        if(checkManager != null) {
            CheckUtils.printWarn(ResultCode.FAILED_MANAGER_IS_EXISTS);
        }
        // 构造对象
        Manager manager = new Manager();
        manager.setManagerName(managerName);
        String salt = MD5Utils.getSalt();
        manager.setSalt(salt);
        String plusPassword = MD5Utils.encrypt(salt, password);
        manager.setPassword(plusPassword);
        Date date = new Date();
        manager.setCreateTime(date);
        manager.setUpdateTime(date);
        manager.setDeleteState((byte) 0);
        manager.setLimitValue(0);
        LimitUtils.grantPower(manager, Rule.ALL_RULE);
        // 调用dao层
        int rows = managerMapper.insertSelective(manager);
        // 校验
        CheckUtils.checkRows(rows, 1, ResultCode.FAILED_CREATE);
        // 打印成功
        log.info(ResultCode.SUCCESS + "  manager id : " + manager.getId());
    }

    @Override
    public Manager login(String managerName, String password) {
        // 校验
        CheckUtils.checkStringParams(ResultCode.FAILED_PARAMS_VALIDATE,
                managerName, password);
        // 校验是否存在
        Manager checkManager = selectManagerByName(managerName);
        if(checkManager == null) {
            CheckUtils.printWarn(ResultCode.FAILED_LOGIN);
        }
        // 验证密码
        if(!MD5Utils.confirm(checkManager.getSalt(), password, checkManager.getPassword())) {
            CheckUtils.printWarn(ResultCode.FAILED_LOGIN);
        }
        // 返回对象
        log.info(ResultCode.SUCCESS + " 管理员登录： id = " + checkManager.getId());
        return checkManager;
    }
}
